<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html>
<head>
<title>Visual Basic lexical structure</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Visual Basic, learn Visual Basic, lexical structure, programming, language">
<meta name="description" content="This part of the Visual Basic tutorial 
covers lexical structure of Visual Basic.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Visual Basic lexical structure</h1>

<p>
Computer languages, like human languages, have a lexical structure. 
A source code of a Visual Basic 
program consists of tokens. Tokens are atomic code elements. 
In Visual Basic, we have comments, variables, 
literals, operators, delimiters and keywords.
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>

<p>
Visual Basic programs are composed of characters from the Unicode 
character set.
</p>


<h2>Comments</h2>

<p>
<code>Comments</code> are used by humans to clarify the source code. All comments in 
Visual Basic follow either the <code>'</code> character or the 
<code>Rem</code> keyword. 
</p>

<pre class="code">
Option Strict On

' This is comments.vb 
' Author: Jan Bodnar
' ZetCode 2010

Module Example

    Rem program starts here
    Sub Main()
        
        Console.WriteLine("This is comments.vb")
        
    End Sub

End Module
</pre>

<p>
Comments are ignored by the Visual Basic compiler.  
</p>


<h2>White space</h2>

<p>
White space in Visual Basic is used to separate tokens in the source file.
It is used to improve readability of the source code. 
</p>

<p>
Dim i As Integer
</p>

<p>
White spaces are required in some places. For example between the <code>Dim</code> 
keyword and the variable name. In other places, it is forbidden. It cannot be present in
variable identifiers or language keywords.
</p>

<pre>
a=1
b = 2
c  =  3
</pre>

<p>
The amount of space put between tokens is irrelevant for the Visual Basic compiler. 
</p>


<h2>Line continuation character</h2>

<p>
We must use a line continuation character, if a statement spans
more than one line. This differs from C and C based languages.
</p>

<pre>
Console.WriteLine("The length of the first string is " _
    + str1.Length.ToString() + " characters")
</pre>

<p>
Mainly for readability reasons, we don't want to have too many characters on one
line. We break the line and continue on the following line. In Visual Basic, we
must use the line continuation character, otherwise the compilation would fail. 
</p>


<h2>Variables</h2>

<p>
A <b>variable</b> is an identifier, which holds a value. In programming 
we say, that we assign a value to a variable. Technically speaking, 
a variable is a reference to a computer memory, where the value is stored. 
Variable names can have alphanumerical characters and underscores. An identifier
may begin with a character or an underscore. It may not begin with a number.
Variable names are not case sensitive. This means, that Name, name or NAME refer
to the same variable. Variable names also cannot match language keywords.
</p>

<pre>
Dim name23 As String
Dim _col As Integer
Dim birth_date As Date
</pre>

<p>
These are valid Visual Basic identifiers.
</p>

<pre class="code">
Option Strict On

Module Example

    Sub Main()

        Dim name As String = "Robert"
        Dim Name As String = "Julia"

        Console.WriteLine(name)
        Console.WriteLine(Name)

    End Sub

End Module
</pre>

<p>
This code will not compile. Identifiers are not case sensitive.
</p>



<h2>Literals</h2>

<p>
A <b>literal</b> is a textual representation of a particular
value of a type. Literal types include Boolean, Integer, floating point, string, 
character, and date. Technically, a literal will be assigned a value 
at compile time, while a variable  will be assigned at runtime.
</p>

<pre>
Dim age As Byte = 29
Dim nationality As String = "Hungarian"
</pre>

<p>
Here we assign two literals to variables. Number 29 and string "Hungarian" are literals. 
</p>

<pre class="code">
Option Strict On

Module Example

    Sub Main()

        Dim sng As Boolean = True
        Dim name As String = "James"
        Dim job As String = Nothing
        Dim age As Byte = 23
        Dim weight As Single = 68.5
        Dim born As DateTime = DateValue("November 12, 1987")

        Console.WriteLine("His name is {0}", name)

        If sng Then
            Console.WriteLine("He is single")
        Else 
            Console.WriteLine("He is in a relationship")
        End If

        Console.WriteLine("His job is {0}", job)
        Console.WriteLine("He weighs {0} kilograms", weight)
        Console.WriteLine("He was born in {0}", _
             Format(born, "yyyy"))

    End Sub

End Module
</pre>

<p>
In the above example, we have other literals. The Boolean literal 
may have value <code>True</code> or <code>False</code>.
<code>James</code> is a string literal.
The <code>Nothing</code> represents the default value 
of any data type. <code>23</code> is an Integer literal. 
<code>68.5</code> is a floating point literal. 
Finally, the <code>November 12, 1987</code> is a date literal.
</p>

<pre>
$ ./literals.exe 
His name is James
He is single
His job is 
He weighs 68.5 kilograms
He was born in 1987
</pre>

<p>
This is the output of the program.
</p>


<h2>Operators</h2>

<p>
An <b>operator</b> is a symbol used to perform an action on some value. 
</p>

<pre>
+    -    *    /    \    ^     &amp;
=    +=   -=   *=   /=   \=    ^=
&lt;    &gt;    &amp;=  &gt;&gt;=   &lt;&lt;=   &gt;=   &lt;= 
&gt;&gt;   &lt;&gt;   &lt;&lt; 
</pre>

<p>
These are Visual Basic operators. We will talk about operators later in the tutorial.
</p>


<h2>Separators</h2>

<p>
A <code>separator</code> is a sequence of one or more characters used 
to specify the boundary between separate, independent regions in plain text or other data stream. 
</p>

<pre>
(   )   {   }   !   #   ,   .   :   :=   ?
</pre>

<p>
These are Visual Basic separators. 
</p>

<pre>
Dim language As String = "Visual Basic"
</pre>

<p>
The double characters are used to mark the beginning and the end of a string. 
</p>


<pre>
Console.WriteLine("Today is {0}", GetToday)
</pre>

<p>
Parentheses (square brackets) are used to mark the method signature. The signature consists 
of method parameters. Curly brackets are used to denote the evaluated value.
</p>

<pre>
Dim array() As Integer = { _
    2, 4, 5, 6, 7, 3, 2 }
</pre>

<p>
The curly brackets are also used to create arrays. 
</p>


<h2>Keywords</h2>

<p>
A keyword is a reserved word in the Visual Basic language. Keywords are used 
to perform a specific task in the computer program. For example, print a value, 
do repetitive tasks or perform logical operations. A programmer cannot use a 
keyword as an ordinary variable. 
</p>

<p>
Visual Basic is rich in keywords. Many of them will be explained in this
tutorial. The keywords include If, Else, Dim, For, Date, Double, Or,
Exit and many others. 
</p>

<!--<p>
The following is a list of Visual Basic keywords.
</p>-->

<!--<pre>
AddHandler AddressOf  Alias    And
AndAlso   As          Boolean  ByRef
Byte      ByVal       Call     Case
Catch     CBool       CByte    CChar
CDate     CDbl        CDec     Char
CInt      Class       CLng     CObj
Const     Continue    CSByte   CShort
CSng      CStr        CType    CUInt
CULng     CUShort     Date     Decimal
Declare   Default     Delegate Dim
DirectCast Do         Double   Each
Else      ElseIf      End      EndIf
Enum      Erase       Error    Event
Exit      False       Finally  For
Friend    Function    Get      GetType
GetXmlNamespace Global GoSub   GoTo
Handles   If          Implements Imports
In        Inherits    Integer  Interface
Is        IsNot       Let      Lib
Like      Long        Loop     Me
Mod       Module      MustInherit MustOverride
MyBase MyClass Namespace Narrowing
New       Next        Not      Nothing
NotInheritable NotOverridable Object Of
On        Operator    Option   Optional
Or        OrElse      Overloads Overridable
Overrides ParamArray  Partial  Private
Property  Protected   Public   RaiseEvent
ReadOnly  ReDim       REM      RemoveHandler
Resume    Return      SByte    Select
Set       Shadows     Shared   Short
Single    Static      Step     Stop
String    Structure   Sub      SyncLock
Then      Throw       To       True
Try       TryCast     TypeOf   UInteger
ULong     UShort      Using    Varint
Went      When        While    Widening
With      WithEvents  WriteOnly Xor
</pre>-->

<pre class="code">
Option Strict On

Module Example

    Sub Main()

        Dim i As Integer

        For i = 0 To 35 Step 5
            Console.WriteLine(i)
        Next

    End Sub
End Module
</pre>

<p>
In the above example, we use several keywords. Option, On, Module, Sub,
Dim, As, Integer, For, To, Step, Next, End are Visual Basic keywords. 
</p>



<p>
In this part of the Visual Basic tutorial, we covered the basic lexis for the Visual Basic
language. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div>
<br>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡ 
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified August 14, 2010  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>

